package day04;

/*
继承
 */
public class Demo02 {
    public static void main(String[] args) {
        Pet pet = new Pet("小花猫", 2, 80);
        System.out.println(pet);

        Dog dog = new Dog("阿黄", 1, 70);
        System.out.println(dog);
        System.out.println(dog.getName()); // 子类继承了父类的方法和属性
        dog.setHealth(100);
        System.out.println(dog);

        Dog dog1 = new Dog("小黑", 2, 90, "狼狗");
        System.out.println(dog1); // 父类的toString不再适用时，子类可以重写这个方法。
        System.out.println(dog1.toString1());
    }
}

/*
父类，宠物类：姓名，年龄，健康度
 */
class Pet {
    private String name;
    private int age;
    private int health;

    // 构造方法、get/set、toString
    public Pet(String name, int age, int health) {
        this.name = name;
        this.age = age;
        this.health = health;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getHealth() {
        return health;
    }

    public void setHealth(int health) {
        this.health = health;
    }

    @Override
    public String toString() {
        return "Pet{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", health=" + health +
                '}';
    }
}

/*
子类，宠物狗类：继承父类的属性和方法。不继承构造方法。
               如果父类有构造方法，子类也需要有构造方法。
 */
class Dog extends Pet {
    private String pinZhong; //品种

    public String getPinZhong() {
        return pinZhong;
    }

    public void setPinZhong(String pinZhong) {
        this.pinZhong = pinZhong;
    }

    public Dog(String name, int age, int health) {
        super(name, age, health); // super表示父类，调用了父类的构造方法
    }

    public Dog(String name, int age, int health, String pinZhong) {
        // super.属性名
        // super.方法名
        // super() 构造方法
        super(name, age, health); // 前面三个属性，调用父类的构造方法
        this.pinZhong = pinZhong;
    }
    // overwrite 重写父类同名的方法
    public String toString1() {
        // 父类的属性，通过调用get方法获取
        return "Dog{name=" + super.getName() + ", age=" +
                super.getAge() + ", health=" + super.getHealth()
                + ", pinZhong=" + this.pinZhong + "}";
    }

    @Override
    public String toString() {
        return "Dog{" +
                "pinZhong='" + pinZhong + '\'' +
                "} " + super.toString();
    }
}

